package wcpackage.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wcpackage.domain.dao.UserDAO;
import wcpackage.domain.entities.User;

import java.util.List;

/**
 * User: rabbit
 * Date: Jun 11, 2009
 * Time: 11:47:05 AM
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    public User logIn(String login, String password) {
        User result = userDAO.findByLoginAndPassword(login, password);
        return result == null ? User.GUEST_USER : result;
    }

    public int numberOfUsers() {
        return (int) userDAO.size();
    }

    public List<User> find(int first, int count, String sortField, String sortType) {
        return userDAO.find(first, count, sortField, sortType);
    }

    public void createNewUser(User user) {
        User newUser = new User(user);
        userDAO.persist(newUser);
        user.clearData();
    }
}
